<?php
/**
 * 评论控制器
 * Created by PhpStorm.
 * User: Jmashimaro
 * Date: 2015/2/13
 * Time: 17:51
 */
class CommentApp extends MallbaseApp {
    /***
     * 获得店铺的评论列表
     * @author zhangjunyang
     * @date   2015/04/01
     * 添加头像路径lizhuang 2015-4-14 15:22:12
     */
    public function index(){
        $config = require ROOT_PATH . '/data/config.qiniu.php';
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }

        /* 取得列表数据 */
        $goods_id = $this->get_gp('goods_id', 'G');
        $offset = $this->get_gp('offset', 'G');
        $size = $this->get_gp('size', 'G');
        $evaluation = $this->get_gp('evaluation','G');
        $offset = $offset ? intval($offset) : 0;
        $size = $size ? intval($size) : 10;
        if($evaluation == 0){
            $where = " ";      //全部评论
        }else{
            $where = " AND evaluation = '{$evaluation}'";      //$evaluation=3为好评、$evaluation=2中评、$evaluation=1差评
        }

        $sql = "select b.buyer_name,b.buyer_id,b.anonymous,a.evaluation_time,a.comment,a.rec_id,
                a.evaluation,a.specification,c.portrait
                from ".DB_PREFIX."order_goods a left join ".DB_PREFIX."order b on a.order_id = b.order_id
                left join ".DB_PREFIX."member c on b.buyer_id =
                c.user_id where b.evaluation_status=1 AND a.goods_id={$goods_id}{$where}
                order by a.evaluation_time desc limit {$offset},{$size}";

        $order_goods_mod = m('ordergoods');
        $comments = $order_goods_mod->getAll($sql);
        $row = array();
        //添加图片完整路径
        foreach ($comments as $key=>$val) {
            $val['portrait'] = $val['portrait'] ? $config['qiniu_url'].$val['portrait'] : Conf::get('default_goods_image');
            $row[$key] = $val;
        }
        $comments = array_merge(array(),$row);
        if ($comments) {
            $this->ajax_return(FLAG_OK, '', $comments);
        } else {
            $this->ajax_return(FLAG_OK, '没有商品评论');
        }
    }

    /***
     * 获得店铺的评论列表
     * @author zhangjunyang
     * @date   2015/04/01
     */
    public function comments(){
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'POST') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        //得到订单ID
        $user_id = $this->checkLogin();
        $order_id = $this->get_gp('order_id', 'P');
        $eval = $this->get_gp('evaluation', 'P');
        $comment = $this->get_gp("comment","P");
        if(!$user_id){
            $this->ajax_return(FLAG_ERROR, '用户没有登录！');
        }
        //验证订单有效性
        $model_order = m('order');
        $order_info  = $model_order->get("order_id={$order_id} AND buyer_id={$user_id}");
        if (!$order_info)
        {
            $this->ajax_return(FLAG_ERROR, '没有这个订单');
        }
        if ($order_info['status'] != ORDER_FINISHED)
        {
            /* 不是已完成的订单，无法评价 */
            $this->ajax_return(FLAG_ERROR, '不是已完成的订单，无法评价');
        }
        if ($order_info['evaluation_status'] != 0)
        {
            /* 已评价的订单 */
            $this->ajax_return(FLAG_ERROR, '已评价的订单');
        }
        //准备插入评价
        $model_ordergoods = m('ordergoods');
        /* 写入评价 */
        if ($eval <= 0 || $eval > 3)
        {
            $this->ajax_return(FLAG_ERROR, '评价错误');

            return;
        }
        switch ($eval)
        {
            case 3:
                $credit_value = 1;
                break;
            case 1:
                $credit_value = -1;
                break;
            default:
                $credit_value = 0;
                break;
        }
        $goods_list = $model_ordergoods->find("order_id={$order_id}");
        foreach($goods_list as $key => $value){
            $sql = "update  ".DB_PREFIX."order_goods
                    set evaluation = ".$eval.",comment='".$comment."',credit_value=".$credit_value.",evaluation_time = ".time()."
                    where rec_id=".$value['rec_id']." and order_id=".$value["order_id"];
            $model_ordergoods->query($sql);
        }
        /* 更新订单评价状态 */
        $model_order->edit($order_id, array(
            'evaluation_status' => 1,
        ));

        /* 更新卖家信用度及好评率 */
        $model_store = m('store');
        $model_store->edit($order_info['seller_id'], array(
            'credit_value'  =>  $model_store->recount_credit_value($order_info['seller_id']),
            'praise_rate'   =>  $model_store->recount_praise_rate($order_info['seller_id'])
        ));

        /* 更新商品评价数 */
        $model_goodsstatistics = m('goodsstatistics');
        $goods_ids = array();
        foreach ($goods_list as $goods)
        {
            $goods_ids[] = $goods['goods_id'];
        }
        //每一件商品的评论都加1
        $model_goodsstatistics->edit($goods_ids, 'comments=comments+1');


        $this->ajax_return(FLAG_OK, '评价成功');
    }


}